<?php
class egoldclass {

	function egoldclass() {
		global $setupdata, $mysqlclass, $paymentclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
		$this->paymentclass=$paymentclass;
	}

	function send_order_to_gateway($data, $post, $id){
		if ($data['PAYMENT_UNITS'] == "97") $data['VK_CURR'] = "LTL";
		elseif ($data['PAYMENT_UNITS'] == "1") $data['VK_CURR'] = "USD";
		elseif ($data['PAYMENT_UNITS'] == "85") $data['VK_CURR'] = "EUR";
		if (isset($data['VK_CURR'])){
			$post['order_amount'] = $this->paymentclass->amount_convert($post['order_amount'], $data['VK_CURR']);
		}
		$data['PAYMENT_ID'] = $id;
		$data['VK_RETURN'] = $this->setupdata['base_href']."/index.php?module=payment&gateway=egold&act=return";
		$data['VK_STAMP'] = $post['order_id'];
		$data['PAYMENT_AMOUNT'] = $post['order_amount'];
		$data['VK_MSG'] = $post['mess']." (".$data['VK_STAMP'].")";
		print_r('
			 <form name="payment" action="https://www.e-gold.com/sci_asp/payments.asp" method="POST" target="_egold">
				<input type="hidden" name="PAYEE_ACCOUNT" value="'.$data['PAYEE_ACCOUNT'].'">
				<input type="hidden" name="PAYEE_NAME" value="'.$data['PAYEE_NAME'].'">
				<input type="hidden" name="PAYMENT_AMOUNT" value="'.$data['PAYMENT_AMOUNT'].'" size=5>
				<input type="hidden" name="PAYMENT_ID" value="'.$data['PAYMENT_ID'].'">
				<input type="hidden" name="PAYMENT_UNITS" value="'.$data['PAYMENT_UNITS'].'">
				<input type="hidden" name="PAYMENT_METAL_ID" value="'.$data['PAYMENT_METAL_ID'].'">
				<input type="hidden" name="NOPAYMENT_URL" value="'.$data['VK_RETURN'].'&result=bad&inv='.$data['VK_STAMP'].'"> 
				<input type="hidden" name="NOPAYMENT_URL_METHOD" value="POST">
				<input type="hidden" name="PAYMENT_URL" value="'.$data['VK_RETURN'].'&result=ok">
				<input type="hidden" name="PAYMENT_URL_METHOD" value="POST">
				<input type="hidden" name="ORDER_ID" value="'.$data['VK_STAMP'].'">
				<input type="hidden" name="BAGGAGE_FIELDS" value="ORDER_ID">
				<input type="hidden" name="CUSTOMERID" value="0">
				<input type="hidden" name="SUGGESTED_MEMO" value="'.$data['VK_MSG'].'">
			 </form>
			<script language="javascript1.4" type="text/javascript">document.payment.submit();</script>
		');
	}
	
	function verify_post($post){
		if (
			(isset($post['PAYEE_ACCOUNT']) && (strlen($post['PAYEE_ACCOUNT'])>4) && (strlen($post['PAYEE_ACCOUNT'])<10)) &&
			(isset($post['PAYMENT_AMOUNT']) && (floatval($post['PAYMENT_AMOUNT'])>0) && (floatval($post['PAYMENT_AMOUNT'])<100000)) &&
			(isset($post['PAYMENT_UNITS']) && (intval($post['PAYMENT_UNITS'])>0) && (intval($post['PAYMENT_UNITS'])<10000)) &&
			(isset($post['PAYMENT_METAL_ID']) && (intval($post['PAYMENT_METAL_ID'])>0) && (intval($post['PAYMENT_METAL_ID'])<5)) &&
			(isset($post['PAYMENT_BATCH_NUM']) && (intval($post['PAYMENT_BATCH_NUM'])>0) && (intval($post['PAYMENT_BATCH_NUM'])<100000000)) &&
			(isset($post['PAYER_ACCOUNT']) && (strlen($post['PAYER_ACCOUNT'])>4) && (strlen($post['PAYER_ACCOUNT'])<10)) &&
			(isset($post['PAYMENT_ID']) && (intval($post['PAYMENT_ID'])>0) && (intval($post['PAYMENT_ID'])<100000)) &&
			(isset($post['payment']) && ($post['payment']=='Continue'))
			){
			return true;
		} else return false;
	}
	
	function order_notify($post){
		if ($this->verify_post($post) === false) return 0;
		$data = $this->paymentclass->get_gateway_data('egold');
		if ($post['PAYMENT_METAL_ID'] == "1") $metal_id = "Gold";
		elseif ($post['PAYMENT_METAL_ID'] == "2") $metal_id = "Silver";
		elseif ($post['PAYMENT_METAL_ID'] == "3") $metal_id = "Platinum";
		elseif ($post['PAYMENT_METAL_ID'] == "4") $metal_id = "Palladium";
		else $metal_id = "error_metal_id";

		if ($post['PAYMENT_UNITS'] == "97") $unit = "LTL";
		elseif ($post['PAYMENT_UNITS'] == "1") $unit = "USD";
		elseif ($post['PAYMENT_UNITS'] == "85") $unit = "EUR";
		else $unit = "error_unit";
		
		$time_start = time()-1900;
		$time_end = time()+1900;
		$curlargs = "?initial=1&PassPhrase=".$data['VK_PASS'].
        "&AccountID=".$data['PAYEE_ACCOUNT'].
        "&startmonth=".date("m", $time_start)."&startday=".date("d", $time_start)."&startyear=".date("Y", $time_start).
        "&endmonth=".date("m", $time_end)."&endday=".date("d", $time_end)."&endyear=".date("Y", $time_end)."&".
        "paymentsreceived=1&paymentsmade=1&inexchanges=1&outexchanges=1&".
        "bailments=1&redemptions=1&fees=1&incentives=1&batchfilter=".$post['PAYMENT_BATCH_NUM'].
        "&counterfilter=&metalfilter=&paymentidfilter=&CURPAGE=0";

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $curlargs);
		curl_setopt($ch, CURLOPT_URL, "https://www.e-gold.com/acct/historycsv.asp");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  0);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$content = curl_exec($ch);
		if (curl_errno($ch)) print curl_error($ch);
		else curl_close($ch);
		$result = explode("\n", $content);
		$status = "BAD";
		for ($i=1; $i<count($result)-1; $i++) {
			print_r($result[$i]);
			if (
				(strpos($result[$i], '"Payment Received"') !== false) &&
				(strpos($result[$i], ','.$post['PAYMENT_BATCH_NUM'].',') !== false) &&
				(strpos($result[$i], ','.$post['PAYMENT_AMOUNT'].',') !== false) &&
				(strpos($result[$i], ',"'.$post['PAYMENT_ID'].'"') !== false) &&
				(strpos($result[$i], ',"'.$unit.'",') !== false) &&
				(strpos($result[$i], ','.$metal_id.',') !== false)
				){
				$status = "OK";
				break;
			} else $status = "BAD";
		}
		if ($status == "OK") $this->paymentclass->set_invoice_status($post['PAYMENT_ID'], 'pay');
		elseif ($status == "BAD") $this->paymentclass->set_invoice_status($post['PAYMENT_ID'], 'error');
		else $this->paymentclass->set_invoice_status($post['PAYMENT_ID'], 'error');
	}
	
	function order_cancel(){
		if ((isset($_GET['inv'])) && (intval($_GET['inv']) > 0)){
			$this->paymentclass->set_invoice_status($_GET['inv'], 'notpay');
		} elseif ((isset($_POST['PAYMENT_ID'])) && (intval($_POST['PAYMENT_ID']) > 0)){
			$this->paymentclass->set_invoice_status($_POST['PAYMENT_ID'], 'notpay');
		}
	}
	
}
?>